﻿

using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using ProductIntory.Domains;
using ProductIntory.Infrastructures.Interface;
using SqlSugar;
using System.Data;
using DbType = SqlSugar.DbType;

namespace ProductIntory.Infrastructures.Implement
{
    //public class DatabaseContext : IDatabaseContext, IDisposable
    //{
    //    private readonly IConfiguration _configuration;
    //    private readonly string _connectionString;
    //    public SqlSugarScope SqlSugar { get; }
    //    public IDbConnection DapperConnection { get; }

    //    public SqlSugarScope SqlSugarScope => throw new NotImplementedException();

    //    public DatabaseContext(IConfiguration configuration)
    //    {
    //        _configuration = configuration;
    //        _connectionString = _configuration.GetConnectionString("Conn");
    //        SqlSugar = new SqlSugarScope(new ConnectionConfig()
    //        {
    //            DbType = DbType.MySql,
    //            ConnectionString = _connectionString,
    //            IsAutoCloseConnection = true,
    //            InitKeyType = InitKeyType.Attribute,
    //        },
    //        db =>
    //        {
    //            db.DbMaintenance.CreateDatabase("WMSRepository");
    //            db.Aop.OnLogExecuting = (sql, pars) =>
    //            {
    //                Console.WriteLine(sql);
    //            };
    //        });
    //        DapperConnection = new MySqlConnection(_connectionString);
    //    }

    //    public void Dispose()
    //    {
    //        DapperConnection?.Dispose();
    //        GC.SuppressFinalize(this);
    //    }
    //    public void InitializeDatabase()
    //    {
    //        try
    //        {
    //            // 初始化所有表
    //            SqlSugar.CodeFirst.InitTables(
    //                      // 你的实体类
    //                      typeof(Users),
    //                      typeof(Role),
    //                      typeof(RoleUser),
    //                      typeof(Exports),
    //                      typeof(DepartMents),
    //                      typeof(Resources),
    //                      typeof(ProductOutAllow)

    //            );

    //            Console.WriteLine("数据库表初始化完成");
    //        }
    //        catch (Exception ex)
    //        {
    //            Console.WriteLine($"数据库初始化失败: {ex.Message}");
    //            throw; // 根据你的需求决定是否抛出异常
    //        }
    //    }
    //}
    public class DatabaseContext : IDatabaseContext
    {
        private readonly IConfiguration _configuration;
        private readonly string _connectionString;
        public SqlSugarScope SqlSugar { get; }
        public IDbConnection DapperConnection { get; }

        public SqlSugarScope SqlSugarScope => throw new NotImplementedException();

        public DatabaseContext(IConfiguration configuration)
        {
            _configuration = configuration;
            _connectionString = _configuration.GetConnectionString("Conn");
            SqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = DbType.SqlServer,
                ConnectionString = _connectionString,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            },
            db =>
            {
                db.DbMaintenance.CreateDatabase("ERP-SaaS");
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);
                };
            });
            DapperConnection = new SqlConnection(_connectionString);
        }


        public void InitializeDatabase()
        {
            try
            {


                // 初始化所有表
                SqlSugar.CodeFirst.InitTables(
                          // 你的实体类
                          //typeof(BomOrder),
                          //typeof(BomPart),
                          //typeof(ProductAttributes),
                          //typeof(ProductInspection),
                          //typeof(ProductionStorage),
                          //typeof(Products),
                          //typeof(ProductStates),
                          //typeof(ProductTypes),
                          //typeof(SalesOutboundTable),
                          //typeof(CheckProductWork)
                //typeof(Useres),
                //typeof(SalesOrderModel)



                );

                Console.WriteLine("数据库表初始化完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"数据库初始化失败: {ex.Message}");
                throw;
            }

        }
    }
}
